package de.hsos.db.main;

import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class Main {
	private static String user = "odwittkoe";
	private static String pass = "dennis123";
	private static String host = "oracle-srv.edvsz.hs-osnabrueck.de";
	private static String port = "1521";
	private static String database = "";
	private final static String server = "ora11";
	private static String driver = "oracle.jdbc.driver.OracleDriver";
	
	
	public static void main(String[] args) {
		String URL = null;
		Connection con = null;
		Statement stmt = null;
		ResultSet rs   = null;
	
		try {
			Class.forName(driver);
			URL = "jdbc:oracle:thin:"+"/@"+host+":"+port+":"+database+server;
			con = DriverManager.getConnection(URL, user, pass);
			stmt = con.createStatement();

			System.out.println("Erstelle Tabelle...");
			String createString = 	"create table Kunde2 ("+
									"KNr INTEGER, "+
									"Name VARCHAR2(30))";
			stmt.executeUpdate(createString);
			System.out.println("Tabelle wurde erstellt.");
			System.out.println();
			System.out.print("Datensätze einfuegen...");
			stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
			rs = stmt.executeQuery("select knr, name from kunde2");
			
			for (int i=0; i<5; i++) {
				rs.moveToInsertRow();	
				rs.updateInt(1, i);
				rs.updateString(2, "Testname"+i);
				rs.insertRow();
				System.out.print(".");
			}
			System.out.println("\nDatensätze wurden eingefuegt.");
		} 
		catch (SQLException e) {
			System.out.println(e.getMessage());
			System.out.println("Fehlercode: "+e.getErrorCode());
			System.out.println("SQL State: "+e.getSQLState());
	    } catch (ClassNotFoundException e) {
			e.printStackTrace();
		} 
		finally {
			//Ressourcen freigeben
			try {
		        if (rs != null) rs.close();
		        if (stmt != null) stmt.close();
		        if (con != null) con.close();
			} 
			catch (Exception e) {
				System.out.println(e.getMessage());
			}
		}
	}
}
